#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>

char* fullpath;
void process_file(char* f);

int main(int argc,char** argv){
	char* fn;
	if(argc<2){
		fn=".";
	}else{
		fn=argv[1];
	}
	
	DIR* dir=opendir(fn);
	if(dir==NULL){
		fprintf(stderr,"open dir %s",fn);
	}
	struct dirent* de;
	int len=_POSIX_PATH_MAX;
	fullpath=malloc(len);
	
	strncpy(fullpath,fn,len);
	printf("path len:%d\n",len);
	
	fullpath[len-1]='\0';
	
	char* ptr;
	while((de=readdir(dir))!=NULL){
		printf("%u %s %d\n",de->d_ino,de->d_name,telldir(dir));
		process_file(de->d_name);
		ptr=fullpath+strlen(fullpath);
		
	}
	
	closedir(dir);
}

void process_file(char* f){
	struct stat statbuf;
	char* ptr=fullpath+strlen(fullpath);
	*ptr++='/';
	*ptr=0;
	
	strcpy(ptr,f);
	
	if(lstat(ptr,&statbuf)<0){
		fprintf(stderr,"error stat %s\n",f);
		return;
	}
	
	if(S_ISDIR(statbuf.st_mode)==0){
		printf("is a file\n");
		
	}else{
		printf("is a dir\n");
	}
	
	ptr[-1]=0;
}

